package cn.chencaiju.exam;

import java.util.Arrays;

import org.junit.Test;

public class ColumnSum {
	public static double sumColumn(double[][] matrix, int column) {
		double sum=0;
		for(int i=0;i<matrix.length;i++) {
			if(column<1&&column>matrix[i].length)break;
			sum+=matrix[i][column-1];
		}
		return sum;
	}
	@Test public void testSumColumn() {
		double[][] matrix={
				{1.5, 2, 3, 4},
				{2.3, 5, 8, 9},
				{3, 5.4, 7, 10}
		};
		System.out.println(sumColumn(matrix,2));
	}
	
	public static double[] sumColumns(double[][] matrix) {
		double[] sums=new double[matrix[0].length];
		for(int j=0;j<matrix[0].length;j++) {
			for(int i=0;i<matrix.length;i++) {
				sums[j]+=matrix[i][j];
			}
		}
		return sums;
	}
	@Test public void testSumColumns() {
		double[][] matrix={
				{1.5, 2, 3, 4},
				{2.3, 5, 8, 9},
				{3, 5.4, 7, 10}
		};
		System.out.println(Arrays.toString(sumColumns(matrix)));		
	}
}
